export default {
	submit() {
		let that = this
		let userId = uni.getStorageSync('userId');
		if (userId == this.product.userId) {
			that.$toast.msg("无法购买自己的产品", {
				icon: 1
			})
			return false
		}
		that.$httpRequest.request({
			that: that,
			method: 'post',
			url: 'wechat/createProductOrder',
			params: that.formData,
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					// that.$toast.msg(res.message, {
					// 	icon: 1
					// })
					that.wechatPay(res.data)
				} else {
					that.$toast.msg(res.message, {
						icon: 1
					})
				}
			}
		})
	},
	wechatPay(id) {
		let that = this
		this.loading = true;
		that.$httpRequest.request({
			that: that,
			url: 'wechat/orderPay',
			params: {
				id
			},
			success: res => {
				that.loading = false
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					// that.upSuccessPay(true)
					let data = res.data
					uni.requestPayment({
						timeStamp: data.timeStamp,
						nonceStr: data.nonceStr,
						package: data.package,
						signType: data.signType,
						paySign: data.paySign,
						success: res => {
							that.$toast.alert({
								content: '支付成功！',
								callback: result => {
									if (result.confirm) {
										uni.navigateBack({
											delta: 1
										})
									}
								}
							})
						}
					})
				} else {
					that.$toast.alert({
						content: res.msg,
						callback: result => {

						}
					})
				}
			}
		})

	},
	changeSize(type) {
		if (type == 'add') {
			this.formData.amount++
		} else {
			if (this.formData.amount > 1) this.formData.amount--;
		}
		this.computedTotal()
	},
	changeAmout() {
		this.computedTotal()
	},
	computedTotal() {
		if (!this.product.freeShipping) {
			this.total = this.product.price * this.formData.amount + this.product.shippingPrice || 0
		} else {
			this.total = this.product.price * this.formData.amount
		}
		if (this.product.receiveCoupon) {
			this.total = this.total - this.product.couponAmount
		}
	},
	//获取指定商品
	getInformationDetail() {
		let that = this
		that.$httpRequest.request({
			that: that,
			method: 'get',
			url: 'wechat/getInformationDetail',
			params: {
				id: that.id
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					that.product = res.data
					if (!that.product.freeShipping) {
						that.total = res.data.price + that.product.shippingPrice || 0
					} else {
						that.total = res.data.price
					}
					if (that.product.receiveCoupon) {
						that.total = that.total - that.product.couponAmount
					}

				}
			}
		})
	},
	//获取所有地址提前默认数据
	getAllAddress() {
		let that = this
		that.$httpRequest.request({
			that,
			url: 'wechat/getAllAddress',
			method: 'get',
			params: {},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					res.data.forEach((item, index) => {
						if (item.isDefault) {
							that.currentAddress = item
							that.formData.addressId = item.id
						}
					})
				} else {

				}
			}
		})
	},
	handlerUpload() {
		let that = this;
		let tempObj = [];
		//上传照片
		that.$httpRequest.handlerUploadPic(res => {
			if (res.code === that.$httpConfig.SUCCESS_CODE) {
				res.tempNameArray.forEach((item, index) => {
					if (item.indexOf(res.data
							.fileOriginName) > 0) {
						tempObj[index] = res.data
					}
					// #ifdef  H5
					tempObj[index] = res.data
					// #endif

				})
				console.log("临时数组=========", tempObj)
				//多照片
				var arr = []
				for (let key in tempObj) {
					// console.log(key, tempObj[key])
					arr.push(tempObj[key])
				}
				// console.log("临时数组=========", tempObj)
				// that.wordList = []
				that.wordList = that.wordList.concat(arr)

				// console.log("去空值后",that.wordList)
				that.wordList = that.unique(that.wordList)
				that.wordList = that.trimSpace(that.wordList)
				console.log("wordList=========", that.wordList)
				var newlist = that.wordList.map((item, index) => {
					if (item) {
						return item = item.id
					}

				})
				that.formData.customFiles = newlist.join(',')

			}
		});

	},
	//删除照片
	del(index) {
		this.wordList.splice(index, 1)
		var newlist = this.wordList.map((item, index) => {
			return item = item.id
		})
		// this.formData.customFiles = newlist.join(',')
	},
	unique(list) {
		let result = {};
		let finalResult = [];
		for (let i = 0; i < list.length; i++) {
			if (list[i]) {
				result[list[i].id] = list[i];
			}

			//因为list[i].id不能重复,达到去重效果,且这里必须知晓"id"或是其他键名
		}
		//现在result内部都是不重复的对象了，只需要将其键值取出来转为数组即可
		for (let item in result) {
			finalResult.push(result[item]);
		}
		//console.log(finalResult);[{id:"羽根",artist:"air"},{name:"id",artist:"周杰伦"}]
		return finalResult;
	},
	trimSpace(array) {
		for (var i = 0; i < array.length; i++) {
			if (array[i] == " " || array[i] == null || typeof(array[i]) == "undefined") {
				array.splice(i, 1);
				i = i - 1;

			}
		}
		return array;
	},
	toAddress() {
		uni.navigateTo({
			url: '/pages/my/address'
		})
	}
}